package com.bosch.in.rtp1.ept.action;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jxls.exception.ParsePropertyException;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

import com.bosch.in.rtp1.ept.constant.ReleasedHoldParameter;
import com.bosch.in.rtp1.ept.manager.ChartManager;
import com.bosch.in.rtp1.ept.manager.HldDurByMonthManager;
import com.bosch.in.rtp1.ept.model.dto.HldDurByMonth;
import com.bosch.in.rtp1.ept.model.technical.Condition4HldDurByMonthDiagram;
import com.bosch.in.rtp1.ept.util.ExporterUtility;
import com.bosch.in.rtp1.ept.util.ManagerFactory;
import com.bosch.in.rtp1.ept.util.SQLConditionBuilder;

public class HldDurChartByMonthAction extends MappingDispatchAction{
	/**
     * Report template file to export to excel
     */
    private static final String EXCEL_TEMPLATE_FILE = "templates/relHldDiagramByMonth_template.xls";
    
	private static final Logger LOGGER = Logger.getLogger(HldDurChartByMonthAction.class);
	
	/**
	 * Response the list of String which represent the path lead to charts
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws SQLException 
	 * @throws IOException 
	 */
	public ActionForward displayHldDurDiagram(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response) throws SQLException, IOException 
			 {
		
		LOGGER.debug("Begin HldDurChartByMonthAction.displayHldDurDiagram");
		// get condition from request
		Condition4HldDurByMonthDiagram cByMonthDiagram = SQLConditionBuilder.extrCondition4HldDurByMonthDiagram(request);
		
		// get HldDurByMonthManager instance
		HldDurByMonthManager hldDurByMonthManager = ManagerFactory.getInstance().getHldDurByMonthManager();  	
		
		// get data for creating chart
		List<HldDurByMonth> hByMonths = hldDurByMonthManager.getHldDurByMonths(cByMonthDiagram);
		
		// get ChartManager instance
		ChartManager chartManager = ManagerFactory.getInstance().getChartManager();
		
		// creating chart list
		List<String> charts = chartManager.getHldDurByMonth(hByMonths, request, response,cByMonthDiagram.getYear());
		
		// send response
		request.setAttribute(ReleasedHoldParameter.HLD_DUR_BY_MONTH_DIAGRAM	, charts);
		request.setAttribute(ReleasedHoldParameter.DIAGRAM_YEAR_HIDDEN_PARAMETER	, cByMonthDiagram.getYear());
		request.setAttribute(ReleasedHoldParameter.DIAGRAM_MONTH_HIDDEN_PARAMETER	, cByMonthDiagram.getMonth());
				
		LOGGER.debug("End HldDurChartByMonthAction.displayHldDurDiagram");
		
    	// Forward to form page
        return mapping.findForward("success");
	}
	
	/**
	 * Response to export to excel click, it will delegate to Manager class
	 * to query the released hold data for chart according to search criteria in request parameter.
	 * The result list then will be save to excel file and returns to client
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws SQLException 
	 * @throws InvalidFormatException 
	 * @throws ParsePropertyException 
	 * @throws IOException 
	 */
	public ActionForward exportDiagramExcel(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response) throws SQLException, ParsePropertyException, InvalidFormatException, IOException 
			 {
	
		LOGGER.debug("Begin HldDurChartByMonthAction.exportDiagramExcel");
		
		// load excel template
		InputStream is = this.getServlet().getServletContext().getResourceAsStream(EXCEL_TEMPLATE_FILE);
		
		// get condition from request
		Condition4HldDurByMonthDiagram cByMonthDiagram = SQLConditionBuilder.extrCondition4HldDurByMonthDiagram(request);
		
		// get HldDurByMonthManager instance
		HldDurByMonthManager hldDurByMonthManager = ManagerFactory.getInstance().getHldDurByMonthManager();  	
		
		// get data for creating chart
		List<HldDurByMonth> hByMonths = hldDurByMonthManager.getHldDurByMonths(cByMonthDiagram);
		
		// MIME type is excel
        response.setContentType("application/vnd.ms-excel");
        
        // offer the user the ability to download and save the resulting Excel file
        response.setHeader("Content-Disposition","attachment; filename=hldDuration.xls");
        
        if (hByMonths.size()>0)
        {
        	// create excel workbook
        	HSSFWorkbook workbook = ExporterUtility.exportDataChart(hByMonths.get(0).getBoxPlotFactors(), is, cByMonthDiagram);
        	ServletOutputStream out = response.getOutputStream();
        	workbook.write(out);
        	out.flush();
        }
        
        LOGGER.debug("End HldDurChartByMonthAction.exportDiagramExcel");
        
        return null;
	}

}
